package com.example.lib_cache.viewmodel;

import android.content.Context;
import android.util.Log;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.lib_cache.model.PhoneAndNickName;
import com.example.lib_cache.network.exception.ApiException;
import com.example.lib_cache.network.exception.ErrorConsumer;
import com.example.lib_cache.network.response.ResponseResultTransformer;
import com.example.lib_cache.service.ApiService;

import io.reactivex.functions.Consumer;

public class EditActivityViewModel extends ViewModel {

    private final static String TAG = EditActivityViewModel.class.getSimpleName();

    private MutableLiveData<Boolean> result = new MutableLiveData<>();

    private String updateInfo = "";

    public void setUpdateInfo(String updateInfo) {
        this.updateInfo = updateInfo;
    }

    public MutableLiveData<Boolean> getResult() {
        return result;
    }

    public void setResult(Boolean result) {
        this.result.postValue(result);
    }

    public void requestChangeNickName(ApiService apiService, Context context){
        if ("".equals(updateInfo)){
            Log.d(TAG,"未编辑信息");
        }
        Log.d(TAG,updateInfo);
        PhoneAndNickName phoneAndNickName = new PhoneAndNickName();
        phoneAndNickName.setNickName(updateInfo);
        apiService.updateUserInfo(phoneAndNickName)
                .compose(ResponseResultTransformer.<String>obtain())
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String s) throws Exception {
                        Log.d(TAG, "修改成功");
                        setResult(true);
                    }
                }, new ErrorConsumer(context) {
                    @Override
                    protected void error(ApiException ex) {
                        Log.d(TAG, "修改失败");
                        Log.d(TAG, ex.getCode());
                        Log.d(TAG, ex.getErrorMsg());
                        if (ex.getCode().equals("501")) {
                            setResult(false);
                        }
                    }
                });
    }

    public void requestChangePhone(ApiService apiService, Context context){
        if ("".equals(updateInfo)){
            Log.d(TAG,"未编辑信息");
        }
        Log.d(TAG,updateInfo);
        PhoneAndNickName phoneAndNickName = new PhoneAndNickName();
        phoneAndNickName.setPhone(updateInfo);
        apiService.updateUserInfo(phoneAndNickName)
                .compose(ResponseResultTransformer.<String>obtain())
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String s) throws Exception {
                        Log.d(TAG, "修改成功");
                        setResult(true);
                    }
                }, new ErrorConsumer(context) {
                    @Override
                    protected void error(ApiException ex) {
                        Log.d(TAG, "修改失败");
                        Log.d(TAG, ex.getCode());
                        Log.d(TAG, ex.getErrorMsg());
                        if (ex.getCode().equals("501")) {
                            setResult(false);
                        }
                    }
                });
    }
}
